package com.mkyong.common.controller;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
 
@Component
public class SearchCommandValidator implements Validator {
 
    private final static String EMPLOYEES_NUMBER = "emplNumber";
 
    @Override
    public boolean supports(Class clazz) {
        return SearchCommand.class.isAssignableFrom(clazz);
    }
 
    @Override
    public void validate(Object target, Errors errors) {
        SearchCommand sc = (SearchCommand) target;
 
//        ValidationUtils.rejectIfEmpty(errors, "name", "shop.name.empty");
//        ValidationUtils.rejectIfEmpty(errors, EMPLOYEES_NUMBER, "shop.emplNumber.empty");
// 
//        if (emplNumber != null && emplNumber < 1)
//            errors.rejectValue(EMPLOYEES_NUMBER, "shop.emplNumber.lessThenOne");
// 
        
        if (sc.getAge() < 0) {
            errors.rejectValue("age", "negativevalue");
        } else if (sc.getAge() > 110) {
            errors.rejectValue("age", "too.darn.old");
        }
    }
}
 
